﻿using EFCore.Sharding;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Util
{
    public class K3CloudHelper
    {
        public HttpClient httpClient;
        public IDbAccessor _db;

        public K3CloudHelper(IDbAccessor db)
        {
            httpClient = new HttpClient();
            _db = db;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public async Task<bool> checkLoginAsync()
        {
            bool isOk = false;
            //HttpClient httpClient = new HttpClient();
            httpClient.Url = "http://219.128.52.26:8876/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
            List<object> Parameters = new List<object>();

            #region 正式环境
            Parameters.Add("666a5eac3f0fff");//帐套Id
            Parameters.Add("王旺");//用户名
            Parameters.Add("1qaz@WSX");//密码 kingdee@123
            Parameters.Add(2052);
            #endregion

            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            var iResult = JObject.Parse(await httpClient.AsyncRequest())["LoginResultType"].Value<int>();
            if (iResult == 1)
            {
                isOk = true;
            }

            return isOk;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data">
        /// data必须为数组：参数格式为List<QueryDataDTO>
        /// </param>
        /// <returns></returns>
        public async Task<List<List<object>>> queryEntityAsync(object data)
        {
            List<List<object>> result = new List<List<object>>();
            try
            {
                bool isOK = await checkLoginAsync();
                if (isOK)
                {
                    //HttpClient httpClient = new HttpClient();
                    httpClient.Url = "http://219.128.52.26:8876/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc";
                    string requestData = JsonConvert.SerializeObject(data);
                    httpClient.Content = requestData;
                    var ret = await httpClient.AsyncRequest();
                    result = (List<List<object>>)JsonConvert.DeserializeObject<List<List<object>>>(ret);
                }
            }
            catch (Exception ex)
            {

            }

            return result;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="FormId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public async Task<ResultEntityDTO> auditEntityAsync(string FormId, AuditModelDTO root)
        {
            ResultEntityDTO result = new ResultEntityDTO();
            try
            {
                //HttpClient httpClient = new HttpClient();
                httpClient.Url = "http://61.143.44.242:8090/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc";
                List<object> ParametersM = new List<object>();
                string data = string.Empty;
                ParametersM.Add(FormId);
                ParametersM.Add(root);
                data = JsonConvert.SerializeObject(ParametersM);
                httpClient.Content = data;
                var ret = await httpClient.AsyncRequest();
                result = (ResultEntityDTO)JsonConvert.DeserializeObject<ResultEntityDTO>(ret);

            }
            catch (Exception ex)
            {

            }

            return result;
        }


        /// <summary>
        /// 下推
        /// </summary>
        /// <param name="FormId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public async Task<ResultEntityDTO> pushEntityAsync(string FormId, RootPushEntityDTO root)
        {
            ResultEntityDTO result = new ResultEntityDTO();
            try
            {
                //HttpClient httpClient = new HttpClient();
                httpClient.Url = "http://61.143.44.242:8090/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Push.common.kdsvc";
                List<object> ParametersM = new List<object>();
                string data = string.Empty;
                ParametersM.Add(FormId);
                ParametersM.Add(root);
                data = JsonConvert.SerializeObject(ParametersM);
                httpClient.Content = data;
                var ret = await httpClient.AsyncRequest();
                result = (ResultEntityDTO)JsonConvert.DeserializeObject<ResultEntityDTO>(ret);
                //if (result.Result.ResponseStatus.IsSuccess == false)
                //{
                //    throw new Exception("下推接口执行失败");
                //}
                //List<SuccessEntitys> successEntitys = new List<SuccessEntitys>();
                //successEntitys = (List<SuccessEntitys>)JsonConvert.DeserializeObject<List<SuccessEntitys>>(result.Result.ResponseStatus.SuccessEntitys.ToString());

            }
            catch (Exception ex)
            {

            }

            return result;
        }


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="FormId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public async Task<ResultEntityDTO> submitEntityAsync(string FormId, SubmitModelDTO root)
        {
            ResultEntityDTO result = new ResultEntityDTO();
            try
            {
                //HttpClient httpClient = new HttpClient();
                httpClient.Url = "http://61.143.44.242:8090/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc";
                List<object> ParametersM = new List<object>();
                string data = string.Empty;
                ParametersM.Add(FormId);
                ParametersM.Add(root);
                data = JsonConvert.SerializeObject(ParametersM);
                httpClient.Content = data;
                var ret = await httpClient.AsyncRequest();
                result = (ResultEntityDTO)JsonConvert.DeserializeObject<ResultEntityDTO>(ret);
            }
            catch (Exception ex)
            {

            }

            return result;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="FormId"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public async Task<InterfaceExecuteResultDTO> updateEntityAsync(string FormId, RootParametersEntityDTO root)
        {
            InterfaceExecuteResultDTO ER = new InterfaceExecuteResultDTO();
            try
            {
                //更新实体
                ///CheckLoginAsync check = new CheckLoginAsync();
                bool isLogin = await checkLoginAsync();
                if (isLogin)
                {
                    //HttpClient httpClient = new HttpClient();
                    httpClient.Url = "http://61.143.44.242:8090/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc";
                    List<object> ParametersM = new List<object>();
                    string data = string.Empty;
                    ParametersM.Add(FormId);
                    ParametersM.Add(root);
                    data = JsonConvert.SerializeObject(ParametersM);
                    httpClient.Content = data;
                    var ret = await httpClient.AsyncRequest();
                    ResultEntityDTO result = (ResultEntityDTO)JsonConvert.DeserializeObject<ResultEntityDTO>(ret);
                    if (result.Result.ResponseStatus.IsSuccess == false)
                    {
                        throw new Exception(result.Result.ResponseStatus.Errors.ToString());
                    }

                    ER.IsSuccess = true;
                }
            }
            catch (Exception ex)
            {
                ER.IsSuccess = false;
                ER.ErrorMsg = ex.Message;
            }

            return ER;
        }
    }
}
